package com.apache.common.plugins;

import com.apache.database.model.Page;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * list列表数据转换tree树形数据统一处理插件
 */
public class CommonListToTreePluginImpl implements CustomMethodPlugin {
    protected Logger log = LoggerFactory.getLogger(getClass());

    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        log.info("【afterInvoke】针对查询返回的结果集，进行转换树形结构..");
        String objName = String.valueOf(map.get("objName"));
        String idField = String.valueOf(map.get("idField"));
        String textField = String.valueOf(map.get("textField"));
        String parentIdField = String.valueOf(map.get("parentIdField"));
        String parentIdVal = StrUtil.doNull(String.valueOf(map.get("parentIdVal")), "0");
        String showTextName = StrUtil.doNull(String.valueOf(map.get("showTextName")), "text");
        String showChildName = StrUtil.doNull(String.valueOf(map.get("showChildName")), "childs");
        if (StrUtil.isNull(objName) || StrUtil.isNull(idField) || StrUtil.isNull(parentIdField)) {
            log.error("因请求参数缺失[objName,idField,textField,parentIdField]，故不进行转换树形结构处理。。");
            return null;
        }
//        "nodeId";
//        String parentIdField="fatherId";
//        String parentIdVal="0";
//        String objName = "eCategory";
        if (map.containsKey("resultObjType")) {
            String resultObjType = String.valueOf(map.get("resultObjType"));
            if ("list".equals(resultObjType)) {
                Object restObj = map.get("resultEntity");
                List<DataMap> listData = new ArrayList<>();
                if (restObj instanceof Page) {
                    Page page = (Page) restObj;
                    listData = page.getPageObjects();
                } else {
                    listData = (List<DataMap>) restObj;
                }
                List<Map> newList = new ArrayList<>();
                if (listData.size() > 0) {
                    newList = getTreeList(listData, objName, idField, textField, parentIdField, parentIdVal,showTextName,showChildName);
                }
                log.info("最终树形数据：" + JSONArray.fromObject(newList));
                map.put(CustomMethodPlugin.afterPluginReturn, newList);
            }
        }
        return null;
    }

    //递归获取数据列表
    private List<Map> getTreeList(List<DataMap> list, String objName, String idField, String textField, String parentIdField, String parentIdVal,String showTextName,String showChildName) {
        List<Map> childList = new ArrayList<>();
        if (list.size() > 0) {
            for (DataMap ds : list) {
                Map objMap = (Map) ds.get(objName);
                Map chMap = new HashMap();
                String idFieldVal = String.valueOf(objMap.get(idField));
                String pVal = String.valueOf(objMap.get(parentIdField));
                if (pVal.equals(parentIdVal)) {
                    chMap.put(showTextName, String.valueOf(objMap.get(textField)));
                    List<Map> nodesList = getTreeList(list, objName, idField, textField, parentIdField, idFieldVal,showTextName,showChildName);
                    if(nodesList.size()>0){
                        chMap.put(showChildName, nodesList);
                    }
                    chMap.put(idField,idFieldVal);
                    chMap.put(parentIdField,parentIdVal);
                    chMap.put(objName,objMap);
                    childList.add(chMap);
                }
            }
        }
        return childList;
    }
}
